package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.*;
import com.mycom.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userSaveItems")
public class UserItemController {

    @Autowired
    private UserFormService userFormService;
    @Autowired
    private FormItemService formItemService;
    @Autowired
    private EmployeeService employeeService;


    @PostMapping
    public Result save(@RequestBody UserForm userForm){
        Employee employee = employeeService.findByIdCard(userForm.getIdCardNumber());
        String formNumber = employee.getDepartmentId() + "-" + employee.getId() + "-" + userForm.getSaveDate().getTime();
        userForm.setFormNumber(formNumber);
        boolean flag1 = userFormService.save(userForm);
        List<FormItem> formItems = userForm.getFormItems();
        boolean flag2 = true;
        for (FormItem formItem : formItems) {
            formItem.setFormId(userForm.getId());
            boolean flag3 = formItemService.save(formItem);
            flag2 = flag2 & flag3;
        }
        boolean flag = flag1 & flag2;
        return new Result(flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR,flag);
    }
    @GetMapping("/{idCardNumber}")
    public Result findByIdCard(@PathVariable String idCardNumber){
        List<UserForm> userForms = userFormService.findByIdCardNumber(idCardNumber);
        for (UserForm userForm : userForms) {
            List<FormItem> formItems = formItemService.findByFormId(userForm.getId());
            userForm.setFormItems(formItems);
        }
        Integer code = userForms!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = userForms!=null ? "查询待提交报表成功！" : "查询待提交报表失败！";
        return new Result(code,userForms,msg);
    }

    @DeleteMapping("/{formNumber}")
    public Result deleteByFormNumber(@PathVariable String formNumber){
        UserForm userForm = userFormService.findByFormNumber(formNumber);
        boolean flag1 = formItemService.deleteByFormId(userForm.getId());
        boolean flag2 = true;
        if (flag1){
            flag2 = userFormService.deleteByFormNumber(formNumber);
        }
        Integer code = flag1 & flag2 ? Code.DELETE_SUCCESS : Code.DELETE_ERR;
        String msg = flag1 & flag2 ? "删除报表成功！" : "删除报表失败！";
        return new Result(code,flag1&flag2,msg);
    }

    @RequestMapping("/deleteMany")
    public Result deleteMany(@RequestBody String[] formNumbers){
        boolean flag1 = true;
        for (int i = 0; i < formNumbers.length; i++) {
            UserForm userForm = userFormService.findByFormNumber(formNumbers[i]);
            flag1 = flag1 & formItemService.deleteByFormId(userForm.getId());
        }
        boolean flag2 = userFormService.deleteMany(formNumbers);
        Integer code = flag1 & flag2 ? Code.DELETE_SUCCESS : Code.DELETE_ERR;
        String msg = flag1 & flag2 ? "批量删除报表成功！" : "批量删除报表失败！";
        return new Result(code,flag1&flag2,msg);
    }
}
